package com.tfy.liquor.service.impl.fileUpload;

import com.tfy.liquor.bean.Result;
import com.tfy.liquor.service.fileUpload.ImageFileService;
import com.tfy.liquor.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class ImageFileServiceImpl implements ImageFileService {
    private final static Logger logger = LoggerFactory.getLogger(ImageFileServiceImpl.class);

    @Override
    public Result uploadImageFile(MultipartFile imageFile) {
        String path = "/www/wwwroot/images";
        String url = null;
        try {
            // 获取当前项目根目录
            path = new File(ResourceUtils.getURL("classpath:").getPath()).getAbsolutePath();
            // 在根目录下创建文件夹
            path += File.separator + "images";
            File file = new File(path);
            url = FileUtil.upLoadFile(imageFile, path);
            logger.info("------>>>>>>uploaded a file successfully!<<<<<<------");
        } catch (IOException e) {
            logger.error("图片上传失败！");
            logger.error(e.getMessage());
            return new Result().error("图片上传失败！");
        }
        Map<String, String> map = new HashMap();
        map.put("url", "http://1.1.1.12:8088/"+url);
        return new Result(map).ok("图片上传成功！");
    }

}
